/******************************************************************************
 * Copyright (c) 2006, Intalio Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Intalio Inc. - initial API and implementation
 *******************************************************************************/

/** 
 * 
 * Date         	Author             Changes 
 * 1 Sep 2006   	MPeleshchyshyn  	Created 
 */
package ms.unicorn.ui.figures;

import ms.unicorn.ui.borders.DiamondSchemeBorder;

import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.geometry.PrecisionPoint;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gmf.runtime.draw2d.ui.figures.IPolygonAnchorableFigure;
import org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure;

/**
 * DiamondFigure class extension. Sets preferred size equal to size specified in
 * setBounds() method if current preferred size is <code>null</code>. This
 * allows to prevent figure resizing as user types label text.
 * 
 * @author MPeleshchyshyn
 * @author <a href="http://www.intalio.com">&copy; Intalio, Inc.</a>
 */
public class EntityDiamondFigure extends NodeFigure implements IPolygonAnchorableFigure {

	private DiamondSchemeBorder border;
	private Theme theme;
	
    public EntityDiamondFigure() {
        super();
        theme = new Theme();
        border = new DiamondSchemeBorder();
        border.setTheme(theme);
        setBorder(border);
    }
    
    @Override
    public PointList getPolygonPoints() {
    	Rectangle r = new Rectangle();
        PointList pointList = new PointList(5);
        Rectangle rt = getBounds().getCopy();
        r.x = rt.x+3;
        r.y = rt.y+2;
        r.width = rt.width - 4;
        r.height = rt.height - 4;
        pointList.addPoint(r.x + r.width / 2, r.y);
        pointList.addPoint(r.x + r.width, r.y + r.height / 2);
        pointList.addPoint(r.x + r.width / 2, r.y + r.height);
        pointList.addPoint(r.x, r.y + r.height / 2);
        pointList.addPoint(r.x + r.width / 2, r.y);
    	return pointList;
    }
    
    @Override
    protected ConnectionAnchor createAnchor(PrecisionPoint p) {
    	if (p!=null)
    		return new SlidableAnchorEx(this, p);
    	return super.createAnchor(p);
    }
    
    @Override
    protected ConnectionAnchor createDefaultAnchor() {
    	return new SlidableAnchorEx(this);
    }
    
    /* (non-Javadoc)
     * @see org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure#getSlidableAnchorArea()
     */
    protected double getSlidableAnchorArea() {
        return 0.7;
    }
    /**
     * Sets the bounds of this Figure to the Rectangle <i>rect</i> and stores
     * these bounds as figure's preferred size when this method is called on
     * object for the forst time. Note that <i>rect</i> is compared to the
     * Figure's current bounds to determine what needs to be repainted and/or
     * exposed and if validation is required. Since {@link #getBounds()} may
     * return the current bounds by reference, it is not safe to modify that
     * Rectangle and then call setBounds() after making modifications. The
     * figure would assume that the bounds are unchanged, and no layout or paint
     * would occur. For proper behavior, always use a copy.
     * 
     * @param rect
     *            The new bounds
     */
    @Override
    public void setBounds(Rectangle rect) {
        Rectangle r = rect.getCopy();
    	r.height = r.width;
    	super.setBounds(r);
    }
    
    @Override
    protected void paintFigure(Graphics graphics) {
        paintShadow(graphics);
        // otherwise stay transparent
    }

    protected void paintShadow(Graphics graphics) {
        super.paintBorder(graphics);
    }

    @Override
    protected void paintBorder(Graphics graphics) {
        //do nothing. we paint the border before the rest of the figure as it is where we paint the shadow.
    }

    @Override
    public IFigure getToolTip() {
    	return super.getToolTip();
        //return ((ActivityEditPart.ActivityFigure) getChildren().iterator().next()).getFigureActivityNameFigure().getToolTip();
    }

	public Theme getTheme() {
		return theme;
	}

	public void setTheme(Theme theme) {
		this.theme = theme;
	}
}
